package AreaService

import (
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/request/BaseReq"
	"gdshop-back-end-go/common/response"
	"gdshop-back-end-go/common/response/AreaResp"
	"github.com/gogf/gf/net/ghttp"
)

// AreaAllLevelList 获取所有级别的地区列表
func AreaAllLevelList(r *ghttp.Request, req *BaseReq.I) *response.JsonResponse {
	ctx := r.GetCtx()
	level := r.GetInt("level", 1)
	if level < 1 || level > 4 {
		return response.FailByRequestMessage(nil, "level 取值1-4")
	}
	var areaList []*AreaResp.AreaTree
	err := toolsDb.GetUnSafaTable(ctx, "area_plus").Fields("code,name").
		Where("is_show = 1 AND LENGTH(`code`) = ?", level*2).Structs(&areaList)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	return response.SuccessByRequestMessageData(nil, "成功", areaList)
}
